package com.yxb.parentProfile.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yxb.common.annotation.Log;
import com.yxb.common.core.controller.BaseController;
import com.yxb.common.core.domain.AjaxResult;
import com.yxb.common.enums.BusinessType;
import com.yxb.parentProfile.domain.ParentProfile;
import com.yxb.parentProfile.service.IParentProfileService;
import com.yxb.common.utils.poi.ExcelUtil;
import com.yxb.common.core.page.TableDataInfo;

/**
 * 家长信息Controller
 * 
 * @author 林德炫
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/parentProfile/parentProfile")
public class ParentProfileController extends BaseController
{
    @Autowired
    private IParentProfileService parentProfileService;

    /**
     * 查询家长联系方式
     */
    @PreAuthorize("@ss.hasPermi('parentProfile:parentProfile:query')")
    @GetMapping("getParentPhone/{userId}")
    public AjaxResult getParentPhone(@PathVariable("userId") Long userId)
    {
        return success(parentProfileService.getParentPhone(userId));
    }

    /**
     * 查询家长信息列表
     */
    @PreAuthorize("@ss.hasPermi('parentProfile:parentProfile:list')")
    @GetMapping("/list")
    public TableDataInfo list(ParentProfile parentProfile)
    {
        startPage();
        List<ParentProfile> list = parentProfileService.selectParentProfileList(parentProfile);
        return getDataTable(list);
    }

    /**
     * 导出家长信息列表
     */
    @PreAuthorize("@ss.hasPermi('parentProfile:parentProfile:export')")
    @Log(title = "家长信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ParentProfile parentProfile)
    {
        List<ParentProfile> list = parentProfileService.selectParentProfileList(parentProfile);
        ExcelUtil<ParentProfile> util = new ExcelUtil<ParentProfile>(ParentProfile.class);
        util.exportExcel(response, list, "家长信息数据");
    }

    /**
     * 获取家长信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('parentProfile:parentProfile:query')")
    @GetMapping(value = "/{parentId}")
    public AjaxResult getInfo(@PathVariable("parentId") Long parentId)
    {
        return success(parentProfileService.selectParentProfileByParentId(parentId));
    }

//    通过user_id查询家长信息（路径改为"/byUserId/{userId}"避免冲突）
    @GetMapping( "/byUserId/{userId}")
    public AjaxResult getInfoByUserId(@PathVariable("userId") Long userId) {
        ParentProfile parentProfile = parentProfileService.selectParentProfileByUserId(userId);
        return parentProfile != null ?
                success(parentProfile) :
                error("家长信息不存在");
    }

    /**
     * 新增家长信息
     */
    @PreAuthorize("@ss.hasPermi('parentProfile:parentProfile:add')")
    @Log(title = "家长信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParentProfile parentProfile)
    {
        // 检查user_id是否已存在
        Long userId = parentProfile.getUserId();
        ParentProfile existing = parentProfileService.selectParentProfileByUserId(userId);
        if (existing != null) {
            return AjaxResult.error("家长信息已存在");
        }
        return toAjax(parentProfileService.insertParentProfile(parentProfile));
    }

    /**
     * 修改家长信息
     */
    @PreAuthorize("@ss.hasPermi('parentProfile:parentProfile:edit')")
    @Log(title = "家长信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParentProfile parentProfile)
    {
        return toAjax(parentProfileService.updateParentProfile(parentProfile));
    }

    /**
     * 删除家长信息
     */
    @PreAuthorize("@ss.hasPermi('parentProfile:parentProfile:remove')")
    @Log(title = "家长信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{parentIds}")
    public AjaxResult remove(@PathVariable Long[] parentIds)
    {
        return toAjax(parentProfileService.deleteParentProfileByParentIds(parentIds));
    }
}
